#coding: utf8
import re
from pygeocoder import Geocoder
from datetime import datetime
import time
import urlparse
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview

class YelpUtils(object):

    utils = None

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            if response.url.find('Barcelona') != 1:
                city="BCN"
            else:
                city="NA"

            restaurant['reviews'] = []                
            restaurant['city']=city
            restaurant['restaurant_name'] = hxs.select('//h1[@itemprop="name"]/text()').extract()
            address0 = hxs.select('//span[@itemprop="streetAddress"]/text()').extract()
            address1 = hxs.select('//span[@itemprop="addressLocality"]/text()').extract()
            address = self.utils.convertListToString(address0) + ", " + self.utils.convertListToString(address1)
            restaurant['address'] = address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['phone'] = self.utils.getPhone(hxs.select('//span[@itemprop="telephone"]/text()').extract())
            restaurant['cp']= hxs.select('//span[@itemprop="postalCode"]/text()').extract()
            restaurant['reviews_total']=self.__getReviews(hxs.select('//div[@itemprop="aggregateRating"]/span/span[@itemprop="reviewCount"]/text()').extract())
            restaurant['score']=self.__getScore(hxs.select('//div[@itemprop="aggregateRating"]/div/meta[@itemprop="ratingValue"]/@content').extract())
            restaurant['url_rest']=None

            """list of types of restaurant"""
            restaurant['rest_type']=self.__getRestType(hxs.select('//span[@class="category-str-list"]'))

            """extract url from yelp redirect"""
            url = hxs.select('//div[@class="biz-website"]/a/@href').extract()
            url = self.utils.convertListToString(url)
            restaurant['url_rest'] = url
            if len(url)>0:
                url = self.__getYelpRedirect(url)
                restaurant['url_rest'] = self.utils.unescape(url)

            """
            self.utils.log("DEBUG", "restaurant filling: " + str(restaurant), self.utils.DEBUG)
            """

            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)


    def fillRestaurantComment(self,response,hxs,restaurant):
        try:

            """
            from scrapy.shell import inspect_response
            inspect_response(response)
            """
            
            
            restaurantReview = RestaurantReview()
            restaurantReview['author'] = hxs.select('div[@class="review-sidebar"]/div[@class="review-sidebar-content"]/div[@class="ypassport media-block clearfix"]/div[@class="media-story"]/ul[@class="user-passport-info"]/li[@class="user-name"]/a/text()').extract()
            if restaurantReview['author']is None or len(restaurantReview['author'])==0:
                #check if ghost author
                restaurantReview['author'] = hxs.select('div[@class="review-sidebar"]/div[@class="review-sidebar-content"]/div[@class="ypassport media-block clearfix"]/div[@class="media-story"]/ul[@class="user-passport-info"]/li[@class="user-name"]/span/text()').extract()
                
            restaurantReview['date'] = self.__getCommentDate(self.utils.convertListToString(hxs.select('div[@class="review-wrapper"]/div[@class="review-content"]/div/span[@class="rating-qualifier"]/text()').extract()))
            restaurantReview['score'] = self.__getScore(hxs.select('div[@class="review-wrapper"]/div[@class="review-content"]/div/div[@itemprop="reviewRating"]/div/meta[@itemprop="ratingValue"]/@content').extract())
            restaurantReview['comment'] = hxs.select('div[@class="review-wrapper"]/div[@class="review-content"]/p[@itemprop="description"]/text()').extract()
              
            restaurant['reviews'].append(restaurantReview)
            self.utils.logProcessDetail("restaurant: " + str(restaurant),self.utils.DEBUG)


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantComment en YELP-UTILS from response: " + str(e),self.utils.ERROR)


    def __getCommentDate(self,txtDate):
        tmp_s=None
        try:
            self.utils.log("DEBUG","------date Post: " + str(txtDate),self.utils.DEBUG)
            if len(txtDate)>0:
                po=txtDate.find("/")
                if po != -1:
                    tmp_s=txtDate[po-2:]
                    bOK=False
                    try:
                        date = datetime.strptime(tmp_s , '%d/%m/%Y')
                        bOK=True
                    except Exception,e:
                        self.utils.log("DEBUG","No se puede convertir fecha, probamos eliminado un carácter",self.utils.DEBUG)
                    if bOK is False:
                        tmp_s=tmp_s[po-1:]
                        date = datetime.strptime(txtDate , '%d/%m/%Y')
                        
                    tmp_s = date.strftime('%d/%m/%Y')
            return tmp_s

        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e),self.utils.ERROR)


    def __getRestType(self,hxs):
        try:
            d=dict()
            for rest_type in hxs.select('a/text()').extract():
                d[self.utils.convertListToString(rest_type)]=0
            return d.keys()      

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)

    def __getScore(self,txtScore):
        tmp_s="0"
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            """tmp_s = str(Decimal(tmp_s)*2)"""
        return tmp_s

    def __getSubScore(self,txtScore):
        tmp_s=""
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            tmp_s = tmp_s.replace("width:","")
            tmp_s = tmp_s.replace("px;","")
            """tmp_s = str(Decimal(tmp_s)/10)"""
        return tmp_s

    def __getReviews(self,txtReviews):
        tmp_rev="0"
        if len(txtReviews)>0:
            tmp_rev=str(int(self.utils.convertListToString(txtReviews)))

            
        return tmp_rev


    def __getYelpRedirect(self, url):
        ini = url.find("url=")
        if ini != -1:
            ini=ini+4
            end = url[ini:].find("&src_")
            if end != -1:
                end = end+ini
                return url[ini:end]
            

        